package com.dycx.cms.converter;

import java.time.LocalDateTime;

import cn.hutool.core.bean.BeanUtil;
import com.dycx.common.vendor.oss.AliyunOss;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.cms.entity.LinkDO;
import com.dycx.cms.vo.LinkVO;
import com.dycx.cms.dto.LinkDTO;

/**
 * 友情链接表(Link)实体转换类
 *
 * @author wuzongbing
 * @since 2025-02-13 03:07:56
 */


@Data
public class LinkConverter implements Serializable {
    public static LinkDO dtoToEntity(LinkDTO dto) {
        if (dto == null) return null;
        LinkDO entity = BeanUtil.toBean(dto, LinkDO.class);
        entity.setThumbnail(AliyunOss.delDomain(entity.getThumbnail()));
        return entity;
    }

    public static LinkVO entityToVo(LinkDO entity) {
        if (entity == null) return null;
        LinkVO vo = BeanUtil.toBean(entity, LinkVO.class);
        vo.setThumbnail(AliyunOss.addDomain(vo.getThumbnail()));
        return vo;
    }

    public static List<LinkVO> entityToVoList(List<LinkDO> entityList) {
        List<LinkVO> voList = new ArrayList<>();
        for (LinkDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<LinkVO> entityToVoPage(Page<LinkDO> page) {

        List<LinkVO> newRecords = new ArrayList<>();

        List<LinkDO> records = page.getRecords();
        for (LinkDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<LinkVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
